//
//  OrderLogisticsVm.swift
//  Pods
//
//  Created by eme on 2017/4/5.
//
//

import RxSwift
import ModelProtocol
import NetWorkCore
import SwiftyJSON

class OrderLogisticsVm{
    
    let getOrderLogistics = PublishSubject<String>()
    
    let backDate = PublishSubject<[LogisticsModel]>()
    
    let disposeBag = DisposeBag()
    
    let error = PublishSubject<MikerError>()
    
    var loading : Observable<Bool>
    
    
    init (){
        
        let requestIndicator = ActivityIndicator()
        self.loading = requestIndicator.asObservable()
        
        let request = getOrderLogistics.map{ orderid in
            
            return MineApi.getOrderLogistics(orderid: orderid)
            
            }.shareReplay(1)
        
        let response = request.emeRequestApiForArray(LogisticsModel.self, activityIndicator: requestIndicator).shareReplay(1)
        
        response.subscribe(onNext:{[unowned self] result in
            
            switch result{
                
            case .success(let data):
                
                self.backDate.onNext(data)
                
            case.failure(let error):
                
                self.error.onNext(error)
                
            }
            
        }).addDisposableTo(disposeBag)
        
    }
    
}
